package com.youlin.controller;

import com.youlin.dto.*;
import com.youlin.dto.req.EditCustomReqDto;
import com.youlin.dto.req.SortCustomReqDto;
import com.youlin.dto.res.CMListResDto;
import com.youlin.dto.res.CustomResDto;
import com.youlin.entities.BygAdmin;
import com.youlin.service.BygCustomService;
import com.youlin.untils.AdminUtil;
import com.youlin.untils.MyBeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * 风俗信息管理
 * @author ${author}
 * @since 2020-06-11
 */
@ApiSort(5)
@Api(tags = {"5 - 风俗信息管理"})
@RestController
@RequestMapping("/bygCustom")
public class BygCustomController {

    @Autowired
    private BygCustomService bygCustomService;

    /**
     * 获取风俗信息数据列表
     * @param baseReqDto
     * @return
     */
    @PostMapping("/findAllCustoms")
    @ApiOperationSort(value = 1)
    @ApiOperation(value = "1 - 获取风俗信息数据列表")
    public PageDataResDto<List<CMListResDto>> findAllCustoms (@RequestBody PageReqDto baseReqDto) {
        PageInfo pageInfo = baseReqDto.getPageInfo();
        BygAdmin currentAdmin = AdminUtil.getCurrentAdmin();
        return bygCustomService.findAllCustoms (currentAdmin.getUnitId(), pageInfo);
    }

    /**
     * 获取风俗信息回显详情数据
     * @param baseReqDto
     * @return
     */
    @PostMapping("/findCustomById")
    @ApiOperationSort(value = 2)
    @ApiOperation(value = "2 - 获取风俗信息回显详情数据", notes = "请求参数：data = 风俗信息ID")
    public BaseDataResDto<CustomResDto> findCustomById (@RequestBody BaseReqDto<Integer> baseReqDto) {
        Integer customId = baseReqDto.getData();
        if (customId == null) return new BaseDataResDto<>(Status.PARAMETERERROR);
        return bygCustomService.findCustomById (customId);
    }

    /**
     * 新建或编辑风俗信息数据
     * @param baseReqDto
     * @return
     */
    @PostMapping("/editCustom")
    @ApiOperationSort(value = 3)
    @ApiOperation(value = "3 - 新建或编辑风俗信息数据",
            notes = "响应参数：status-message = {1035-风俗信息标题重复}")
    public BaseResDto editCustom (@RequestBody BaseReqDto<EditCustomReqDto> baseReqDto) {
        EditCustomReqDto data = baseReqDto.getData();
        BygAdmin currentAdmin = AdminUtil.getCurrentAdmin();
        if (MyBeanUtil.isParamError(data)) return new BaseResDto<>(Status.PARAMETERERROR);
        if ((StringUtils.isBlank(data.getVideoUrl()) || StringUtils.isBlank(data.getVideoCoverUrl()))
                && MyBeanUtil.isEmpty(data.getPictureUrls())) return new BaseResDto<>(Status.PARAMETERERROR);
        return bygCustomService.editCustom (currentAdmin.getUnitId(), data);
    }

    /**
     * 编辑风俗信息排序
     * @param baseReqDto
     * @return
     */
    @PostMapping("/editCustomNo")
    @ApiOperationSort(value = 4)
    @ApiOperation(value = "4 - 编辑风俗信息排序")
    public BaseResDto editCustomNo (@RequestBody BaseReqDto<SortCustomReqDto> baseReqDto) {
        SortCustomReqDto data = baseReqDto.getData();
        if (MyBeanUtil.isParamError(data)) return new BaseResDto<>(Status.PARAMETERERROR);
        BygAdmin currentAdmin = AdminUtil.getCurrentAdmin();
        return bygCustomService.editCustomNo (currentAdmin.getUnitId(), data.getCustomId(), data.getToUp());
    }

    /**
     * 删除风俗信息
     * @param baseReqDto
     * @return
     */
    @PostMapping("/delCustom")
    @ApiOperationSort(value = 5)
    @ApiOperation(value = "5 - 删除风俗信息", notes = "请求参数：data = 风俗信息ID")
    public BaseResDto delCustom (@RequestBody BaseReqDto<Integer> baseReqDto) {
        Integer customId = baseReqDto.getData();
        if (customId == null) return new BaseResDto<>(Status.PARAMETERERROR);
        return bygCustomService.delCustom (customId);
    }

}

